#import "TaGuanZhuDetailViewController.h"
#import "Tools.h"
#import "ConfigData.h"
#import "TaGuanZhuYeDianCell.h"
#import "NearbyTableViewCell.h"
#import "TaGuanZhuLiWuCell.h"
#import "ASIFormDataRequest.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#import "JSON.h"
@interface TaGuanZhuDetailViewController (){
    
    UISearchBar *_searchBar;
}

@end

@implementation TaGuanZhuDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    list=[[NSMutableArray alloc] initWithCapacity:0];
    
    
    if (_taGuanZhuType==TaGuanZhuTypeFenSi) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        _searchBar.placeholder = @"搜索好友";
        _searchBar.tintColor = [UIColor lightGrayColor];
        _table.tableHeaderView = _searchBar;
    }
    //
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        NSString*  dynamicURL=HOST;
        dynamicURL=[dynamicURL stringByAppendingString:@"index.php/AppAttention/findAttentionMerchantList"];
        NSLog(@"保存dynamicURL==%@",dynamicURL);
        NSURL* url=[[NSURL alloc] initWithString:dynamicURL];
        ASIFormDataRequest *req=[ASIFormDataRequest requestWithURL:url];
        NSString*  uid=self.dic[@"uid"];
        [req addPostValue:[NSString stringWithFormat:@"%@",uid] forKey:@"uid"];
        [req startSynchronous];
        NSError *error = [req error];
        if (!error) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [MBProgressHUD hideHUDForView:app.window animated:YES];
            
            NSString *response = [req responseString];
            NSLog(@"保存返回结果%@",response);
            NSDictionary* dic=[response JSONValue];
            list=dic[@"list"];
            if(list!=nil&&list.count>0){
                [_table reloadData];
            }
            
            
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
            [_table reloadData];
        });
    });

    
}
- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - table


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_taGuanZhuType==TaGuanZhuTypeJiuDian) {
        return 110;
    }
    else if(_taGuanZhuType==TaGuanZhuTypeFenSi){
        
        return 70;
    }
    else{
        
        return 120;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (_taGuanZhuType==TaGuanZhuTypeJiuDian) {
        NSDictionary*  dic=[list objectAtIndex:indexPath.row];
        NSString*  name=dic[@"name"];
        //酒店
        TaGuanZhuYeDianCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TaGuanZhuYeDianCell" forIndexPath:indexPath];
       // cell.imageV //设置图片
        cell.nameLab.text = [NSString stringWithFormat:@"%@",name];
        //cell.rateImageView.image //设置酒店等级
        cell.distanceLab.text = @"8.5km";
        cell.adressLab.text = @"酒店地址酒店地址m啊打发打发";
        return cell;
    }
    else if(_taGuanZhuType==TaGuanZhuTypeFenSi){
        
        
        //粉丝
        NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NearbyTableViewCell" forIndexPath:indexPath];
        //cell.headImageView.image //设置头像
        cell.nameLab.text = @"大哥大";//设置名字
        //cell.sexImageView.image //设置行别的图片
        cell.desLab.text = @"在我遇到你之前，你都要好好的";
        cell.distanceLab.text = @"4.5km";
        cell.timeLab.text = @"6-28 24:00";
        return cell;
    }
    else{
        
        //礼物
        TaGuanZhuLiWuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TaGuanZhuLiWuCell" forIndexPath:indexPath];
        //cell.imageV //设置图片
        cell.titleLab.text = @"200颗砖石";
        cell.priceLab.text = @"10元";
        cell.timeLab.text = @"2014-06-20";
        cell.whoLab.text = @"小哥小"; //谁送的
        cell.mingxingzhiLab.text = @"明星值+10";
        return cell;
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}



@end
